package org.openkoala.koala.popup; 

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IActionDelegate;
import org.openkoala.koala.console.ConsoleFactory;
import org.openkoala.koala.exception.KoalaException;
import org.openkoala.koala.mojo.KoalaModuleUpdate;
import org.openkoala.koala.util.ProjectQueryUtil;
import org.openkoala.koala.widget.Module;
import org.openkoala.koala.widget.ModuleUpdate;
import org.openkoala.koala.widget.Project;
import org.openkoala.koala.wizard.wizardpage.component.AddModuleDialog;


public class UpdateModuleAction extends BaseAction {

	/**
	 * Constructor for Action1.
	 */
	public UpdateModuleAction() {
		super();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		ConsoleFactory.showConsole();
		//修改模块的调用
		final IResource resource = getResource();
		String selectedPath = resource.getLocationURI().getPath();
		try {
			Project project  = ProjectQueryUtil.getProject(selectedPath);
			Module module = ProjectQueryUtil.getModule(selectedPath, project);
			final ModuleUpdate moduleUpdate = new ModuleUpdate();
			moduleUpdate.setModuleName(module.getModuleName());
			moduleUpdate.setProjectPath(project.getPath() + "/" + project.getAppName());
			AddModuleDialog addModuleDialog = new AddModuleDialog(shell, SWT.NONE);
			addModuleDialog.setModule(module);
			addModuleDialog.setModuleUpdate(moduleUpdate);
			addModuleDialog.setOperation("edit");
			addModuleDialog.setProject(project);
			addModuleDialog.open();
			
			if (addModuleDialog.getModule()  == null) {
				return;
			}
			
			ProgressMonitorDialog monitorDialog = new ProgressMonitorDialog(shell);
			
				try {
					monitorDialog.run(true, false, new IRunnableWithProgress() {
						
						@Override
						public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
							monitor.beginTask("Running refresh project operation!", 2);
							moduleUpdate(moduleUpdate);
							monitor.worked(1);
							try {
								resource.refreshLocal(IResource.DEPTH_INFINITE, null);
							} catch (CoreException e) {
								e.printStackTrace();
							}
							monitor.worked(2);
							monitor.done();
						}
					});
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			
		} catch (KoalaException e) {
			e.printStackTrace();
			MessageDialog.openError(shell, "ERROR", e.getMessage());
		}
		
	}

	/**
	 * 模块更新
	 * @param moduleUpdate
	 */
	private void moduleUpdate(ModuleUpdate moduleUpdate) {
		KoalaModuleUpdate koalaModuleUpdate = new KoalaModuleUpdate();
		try {
			koalaModuleUpdate.updateModule(moduleUpdate);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
